<?php
namespace app\index\controller;

use BsPaySdk\BsPay;
use BsPaySdk\config\MerConfig;
use BsPaySdk\ScanPayment;
use ChannelMiniWechat;
use think\Controller;
use think\Db;
use think\Validate;
use think\Log;
use think\Cache;
//define("PROD_MODE", false);//联调测试环境
// require_once  dirname(__FILE__). "/../BsPaySdk/init.php";
// BsPay::init(dirname(__FILE__). '/config/config_merch_default.json', false);
// require_once dirname(__FILE__) . "/loader.php";
vendor('BsPaySdk.init');
class Pay extends Controller
{
    public function index(){
        $config = new MerConfig();

        BsPay::init(json_decode(json_encode($config), true), true);

        $uid=$_POST['uid'];
        if(!$uid){
            apiReturn(2,'会员ID不能为空');
        }
        $openid=db('user')->where(['id'=>$uid])->value('openid');
		if(!$openid){
            apiReturn(2,'请绑定微信');
        }
        $scanPayment = new ScanPayment();
        //var_dump($scanPayment);die;
        $result = $scanPayment->create(
        "6666000112671765",
            "0.01",
            "企业续费",
        //    new ChannelJSAlipay("2088522648691973x")
            new ChannelMiniWechat($openid,"wxbc7aca851ac61a98")
        );

        //echo "---> 聚合正扫支付: \n";
        if (!$result || $result->isError()){  //失败处理
            var_dump($result -> getErrorInfo());
        } else {    //成功处理
            $data=$result->getRspDatas();

			
            apiReturn(1,'成功',$data);
        }
    }
    public function notice(){
        // $xml_data = file_get_contents("php://input");
        // $data = json_encode(simplexml_load_string($xml_data, 'SimpleXMLElement', LIBXML_NOCDATA));
        // $result = json_decode($data, true);//转成数组


        parse_str(file_get_contents("php://input"), $data);
        $resp_data = json_decode(json_encode($data, JSON_UNESCAPED_UNICODE), true)['resp_data'];
        $order_sn = json_decode($resp_data, true)['req_seq_id'];
		$bank_code = json_decode($resp_data, true)['bank_code'];
        $file='21888.txt';
        file_put_contents($file,$order_sn);
        $file='21999.txt';
        file_put_contents($file,$bank_code);
        // $input = Request()->post();

        if ($bank_code == 'SUCCESS') {
				$status=db('order')->where(['order_number'=>$order_sn])->value('status');
				
				$number=Cache::get($order_sn); 
				if($status==1 && !$number){
				Cache::inc($order_sn);	

                $company_id=db('order')->where(['order_number'=>$order_sn])->value('company_id');
				

                $market_user_id=db('company_introduction')->where(['id'=>$company_id])->value('market_user_id');
                $device_count=db('seal')->where(['company_id'=>$company_id])->count();
                //print_r($market_user_id);die;
                $time = time();
                $auth_data = [
                    md5('2s0e2a1lD_amvaerakpeit' . date('Y/m/d')) => md5(date('Y/m/d') . '9a950aa3ef6d130d'),
                    'timestamp' => $time,
                    'company_id'=>$company_id,
                    'market_user_id'=>$market_user_id,
                    'device_count'=>$device_count,
                    'device_single_price'=>C('SEAL_PRICE'),
                ];
                $auth_result = httpsRequest('https://zssw.hnwcft.com' . '/Index/Auth/getSign', $auth_data);
                $auth_result = json_decode($auth_result, true);
                //return $auth_result['data'];
                //if ($auth_result['result'] == 1) {

                    $a = md5('2s0e2a1lD_amvaerakpeit' . date('Y/m/d'));
                    $b = md5(date('Y/m/d') . '9a950aa3ef6d130d');
                    $data[$a] = $b;
                    $data['sign'] = md5($auth_result['data'] . ($time + 3));
                    $bb = httpsRequest('https://zssw.hnwcft.com' . '/Index/Auth/yearService', $data);
                    $cc=json_decode($bb, true);
					file_put_contents('411.txt',print_r($cc,true));
                    //if($cc['result']==1){
                        //续费成功+1年
                        $time=db('company_introduction')->where(['id'=>$company_id])->value('service_expiration_time');
                        if($time>time()){
                            //过期时间大于现在，就在过期时间上延长
                            $service_expiration_time=strtotime("+1year",$time);
                        }else{
                            //过期时间小于于现在，就在现在的时间上延长
                            $service_expiration_time=strtotime("+1year");
                        }
						file_put_contents('422.txt',$service_expiration_time);
                        $res=db('company_introduction')->where(['id'=>$company_id])->update(['service_expiration_time'=>$service_expiration_time]);
						if($res){
							db('order')->where(['order_number'=>$order_sn])->update(['status'=>2]);
							$reply = "RECV_ORD_ID_".$order_sn;
							echo $reply;
							die();
						}else{
							$reply = "回调处理失败";
							echo $reply;
							die();
						}



						
                    //}
                    //return $bb;
                //}

			}


        }
		
    }

}
